package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskClass;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskClassRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.util.DateUtils;

/**
 * 查看作业详情
 */
@CrossOrigin
@RestController
public class WorkInfoController extends YxyApiBaseController {

    @Autowired
    HomeworkTaskClassRepository homeworkTaskClassRepository;

    @Autowired
    HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Autowired
    HomeworkTaskQuesRepository homeworkTaskQuesRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/workinfo/get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestParam String taskId, //
            @RequestParam String classId, //
            ReqModel reqModel, //
            RespModel respModel) throws ParseException {
        // ##
        // 获取用户基本信息
        try {
            reqModel.userId = getProfile(request, response).getId();
            reqModel.schoolId = getProfile(request, response).getSchoolId();
        } catch (Exception e) {
            Assert.isTrue(false, "用户信息异常，请重新登录。");
        }
        reqModel.taskId = taskId;
        // 获取班级详情
        HomeworkTaskClass homeworkTaskClass = homeworkTaskClassRepository.findTop1ByTaskIdAndClassId(taskId, classId);
        // 获取班级未交作业学生 -1-逾期未提交,0-未做,1-完成 ,2-已批阅,3-答题中
        List<Integer> statusOfFinished = new ArrayList<Integer>();
        statusOfFinished.add(-1);
        statusOfFinished.add(0);
        statusOfFinished.add(3);
        List<HomeworkTaskReport> homeworkTaskReports = homeworkTaskReportRepository
                .findByTaskIdAndClassIdAndStatusOfFinishedInOrderByRightRateDesc(taskId, classId, statusOfFinished);
        List<Student> students = new ArrayList<Student>();
        homeworkTaskReports.forEach(homeworkTaskReport -> {
            Student student = new Student();
            student.studentId = homeworkTaskReport.getStudentId();
            student.studentNick = homeworkTaskReport.getStudentNick();
            students.add(student);
        });
        // 试题总数获取
        Integer quesTotal = homeworkTaskQuesRepository.countByTaskId(reqModel.taskId);
        // 是否有通知类试题
        Integer count1 = homeworkTaskQuesRepository.countByTaskIdAndQuesType(reqModel.taskId, "2");
        boolean isNotify = (count1 == null ? false : (count1 > 0 ? true : false));
        // return
        respModel.taskId = homeworkTaskClass.getTaskId();
        respModel.taskName = homeworkTaskClass.getTitle();
        respModel.classId = homeworkTaskClass.getClassId();
        respModel.grade = this.getGradeValue(homeworkTaskClass.getGrade());
        respModel.className = homeworkTaskClass.getClassName();
        respModel.publishDate = DateUtils.formatDate3(homeworkTaskClass.getPublishDate());
        respModel.predictDate = DateUtils.formatDate3(homeworkTaskClass.getPredictDate());
        respModel.numOfStudent = homeworkTaskClass.getNumOfStudent();
        respModel.submitOfStudent = homeworkTaskClass.getSubmitOfStudent();
        respModel.students = students;
        respModel.quesTotal = quesTotal == null ? 0 : quesTotal;
        respModel.isNotify = isNotify;
        return respModel;
    }

    /** 请求参数 **/
    public static class ReqModel extends YxyApiReqModel {
        // 任务Id
        public String taskId;
        // 班级Id
        public String classId;
        // 用户Id (不用传参)
        public String userId;
        // 学校Id (不用传参)
        public String schoolId;
    }

    /** 响应 **/
    public static class RespModel extends YxyApiRespModel {
        // 任务Id
        public String taskId;
        // 任务名称
        public String taskName;
        // 班级Id
        public String classId;
        // 班级名称
        public String className;
        // 年级
        public String grade;
        // 发布时间
        public String publishDate;
        // 截止时间
        public String predictDate;
        // 参与任务的人数
        public int numOfStudent;
        // 批阅人数
        public int submitOfStudent;
        // 未完成人名单列表
        public List<Student> students;
        // 是否有通知类试题
        public boolean isNotify = false;
        // 本次作业试题总数
        public int quesTotal = 0;
    }

    // 未完成的人名单
    public static class Student {
        // 学生Id
        public String studentId;
        // 学生昵称
        public String studentNick;
    }

    // 01年级~12年级
    public static String getGradeValue(String grade) {
        if (grade == null) { return "暂无年级"; }
        grade = grade.toLowerCase();
        if (grade.equals("g1")) {
            return "一年级";
        } else if (grade.equals("g2")) {
            return "二年级";
        } else if (grade.equals("g3")) {
            return "三年级";
        } else if (grade.equals("g4")) {
            return "四年级";
        } else if (grade.equals("g5")) {
            return "五年级";
        } else if (grade.equals("g6")) {
            return "六年级";
        } else if (grade.equals("g7")) {
            return "七年级";
        } else if (grade.equals("g8")) {
            return "八年级";
        } else if (grade.equals("g9")) {
            return "九年级";
        } else if (grade.equals("g10")) {
            return "高一";
        } else if (grade.equals("g11")) {
            return "高二";
        } else if (grade.equals("g12")) {//
            return "高三";//
        } else if (grade.equals("gc4")) {//
            return "九年级";//
        }
        return grade;
    }
}
